package cn.chen.controller;

import cn.chen.model.Meeting;
import cn.chen.model.Participation;
import cn.chen.model.User;
import cn.chen.service.MeetingService;
import cn.chen.service.ParticipationService;
import cn.chen.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbpu
 * @since 2023-05-28
 */
@RestController
@RequestMapping("/meeting")
public class MeetingController {
    @Autowired
    private MeetingService meetingService;

    @Autowired
    private ParticipationService participationService;

    @Autowired
    private UserService userService;
    //添加会议
    @PostMapping("/add")
    public Meeting addMeeting(@RequestBody Meeting meeting){
        //填充会议发布时间
        meeting.setPublicTime(String.valueOf(LocalDateTime.now()));
        meetingService.save(meeting);
        //根据会议名称查询会议
        LambdaQueryWrapper<Meeting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Meeting::getTitle, meeting.getTitle());
        //根据时间倒叙
        wrapper.orderByDesc(Meeting::getPublicTime);
        return meetingService.getOne(wrapper);
    }

    //分页查询会议列表
    @GetMapping("/list")
    public List<Meeting> list(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer size){
        LambdaQueryWrapper<Meeting> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Meeting::getPublicTime);
        Page<Meeting> meetingPage = meetingService.page(new Page<>(page, size), wrapper);
        return meetingPage.getRecords();
    }

    //根据id查询会议
    @GetMapping("/getMeetingById")
    public Meeting getMeetingById(@RequestParam Integer id){
        return meetingService.getById(id);
    }

    //根据用户id查询会议
    @GetMapping("/getMeetingByUserId")
    public Page<Meeting> getMeetingByUserId(@RequestParam Integer userId,
                                            @RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer size,
                                            @RequestParam(required = false) String title){
        //根据uid在参会表中查询会议id
        LambdaQueryWrapper<Participation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Participation::getUid, userId);
        List<Participation> participationList = participationService.list(wrapper);

        List<Integer> meetingIdList = participationList.stream().map(Participation::getMid).collect(Collectors.toList());


        //根据会议id查询会议
        LambdaQueryWrapper<Meeting> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(Meeting::getId, meetingIdList);
        if (title != null) {
            wrapper1.like(Meeting::getTitle, title);
            return meetingService.page(new Page<>(1, 20), wrapper1);
        }
        return meetingService.page(new Page<>(page, size), wrapper1);

    }

    //根据会议id查询会议参会人员信息
    @GetMapping("/getParticipationByMeetingId")
    public List<User> getParticipationByMeetingId(@RequestParam Integer meetingId){
        LambdaQueryWrapper<Participation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Participation::getMid, meetingId);
        List<Participation> participationList = participationService.list(wrapper);
        List<Integer> uidList = participationList.stream().map(Participation::getUid).collect(Collectors.toList());
        LambdaQueryWrapper<User> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(User::getId, uidList);
        return userService.list(wrapper1);
    }

}
